<?php

/**
 * TaskForm
 * @property-read Task[] $task
 */
class TaskForm extends CFormModel
{
	public $status;
	private $_tasks;

	public function rules()
	{
		return array(
			array('status', 'type', 'type' => 'array'),
		);
	}

	public function getTasks()
	{
		if (!is_array($this->_tasks)) {
			$this->_tasks = array();
			foreach (Task::taskLabels() as $name => $label) {
				$this->_tasks[$name] = Task::load($name);
			}
		}
		return $this->_tasks;
	}

	public function save()
	{
		if ($this->validate()) {
			try {
				$tasks = $this->getTasks();
				foreach ($this->status as $key => $value) {
					if (isset($tasks[$key]) && $tasks[$key]->isSubmitAble) {
						$tasks[$key]->status = $value;
					}
				}
				return true;
			} catch (Exception $e) {
				$this->addError('status', $e->getMessage());
			}
		}
		return false;
	}
}
